package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.user.model.common.dtos.PageResponseResult;
import com.heima.user.model.common.dtos.ResponseResult;
import com.heima.user.model.common.enums.AppHttpCodeEnum;
import com.heima.user.utils.common.WmThreadLocalUtil;
import com.heima.wemedia.dtos.WmMaterialDto;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.pojos.WmMaterial;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.UUID;

@Service
@Slf4j
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
    @Autowired
    private FileStorageService fileStorageService;

    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {

        //检查参数是否为空
        if (multipartFile == null || multipartFile.getSize() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

            //获取文件名
            String Originafilename = multipartFile.getOriginalFilename();

            //获取后缀
            String substring = Originafilename.substring(Originafilename.lastIndexOf("."));

            //生成文件名
            String filename = UUID.randomUUID().toString();

            String url = null;
        try {
            //上传图片到minio
          url = fileStorageService.uploadImgFile("", filename+substring,multipartFile.getInputStream());

        } catch (Exception e) {
            log.error("上传图片失败");
            return ResponseResult.errorResult(AppHttpCodeEnum.IMAGE_UPLOAD_FAIL);
        }

        //保存到数据库
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUrl(url);
        wmMaterial.setType((short) 0);
        wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setCreatedTime(new Date());
        save(wmMaterial);

        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        //参数校验
        dto.checkParam();

        //分页查询
        Page<WmMaterial> page = new Page<>(dto.getPage(), dto.getSize());

        //查询条件
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();

        //判断是否收藏
        if (dto.getIsCollection() != null && dto.getIsCollection() == 1){
            queryWrapper.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        }

        //根据用户id查询
        queryWrapper.eq(WmMaterial::getUserId,WmThreadLocalUtil.getUser().getId());

        //按照时间排序
        queryWrapper.orderByDesc(WmMaterial::getCreatedTime);

        //查询
        page = page(page, queryWrapper);

        //封装数据
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }
}
